package com.lovo.homework.dao.impl;

import com.lovo.homework.bean.UserBean;
import com.lovo.homework.dao.UserDao;
import com.projec.bean.LinkBean;

import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    private List<UserBean> list = new ArrayList<>();
    private UserBean userBean = null;

    @Override
    public void addUser(UserBean user) {
        try {
            this.setCon();
            ps = con.prepareStatement("insert into t_user(u_name,u_psw,u_birthday,u_level)" +
                    "values (?,?,?,?)");
            ps.setObject(1, user.getName());
            ps.setObject(2, user.getPsw());
            ps.setObject(3, user.getBirthday());
            ps.setObject(4, user.getLevel());
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
    }

    @Override
    public void del(int id) {
        try {
            this.setCon();
            ps = con.prepareStatement("delete from t_user where pk_userId=?");
            ps.setObject(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
    }

    @Override
    public void update(int id, String psw, String level) {
        try {
            this.setCon();
            ps = con.prepareStatement("update  t_user set u_psw=?,u_level=? where pk_userId=?");
            ps.setObject(1, psw);
            ps.setObject(2, level);
            ps.setObject(3, id);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
    }

    @Override
    public List<UserBean> findAll() {
        try {
            this.setCon();
            ps = con.prepareStatement("select * from t_user");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(cv());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return list;
    }

    @Override
    public UserBean findById(int id) {
        try {
            this.setCon();
            ps = con.prepareStatement("select * from t_user where pk_userId=?");
            ps.setObject(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                userBean = cv();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return userBean;
    }

    @Override
    public List<UserBean> findByName(String name) {
        try {
            this.setCon();
            ps = con.prepareStatement("select * from t_user where u_name like?");
            ps.setObject(1, "%" + name + "%");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(cv());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return list;
    }

    @Override
    public List<UserBean> findByLocalDate(LocalDate begin, LocalDate end) {
        try {
            this.setCon();
            ps = con.prepareStatement("select * from t_user where u_birthday>=? and u_birthday<=?");
            ps.setObject(1, begin);
            ps.setObject(2, end);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(cv());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return list;
    }

    @Override
    public List<UserBean> findByLevel(String level) {
        try {
            this.setCon();
            ps = con.prepareStatement("select * from t_user where u_level=?");
            ps.setObject(1, level);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(cv());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return list;
    }

    @Override
    public UserBean findLogin(String name, String psw) {
        try {
            this.setCon();
            ps = con.prepareStatement("select * from t_user where u_name=? and u_psw=?");
            ps.setObject(1, name);
            ps.setObject(2, psw);
            rs = ps.executeQuery();
            if (rs.next()) {
                userBean = cv();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return userBean;
    }

    @Override
    public List<UserBean> findByItem(String name, LocalDate begin, LocalDate end) {

        String sql="select * from t_user where 1=1 ";
        List valueList=new ArrayList<>();
        if (name!=null){
            String like="and u_name like ? ";
            sql+=like;
            valueList.add("%"+name+"%");
        }
        if (begin!=null){
            sql+="and u_birthday >= ? ";
            valueList.add(begin);
        }
       if (end!=null){
           sql+="and u_birthday <= ? ";
           valueList.add(end);
       }
        try{
            setCon();
                ps = con.prepareStatement(sql);
            for (int i = 0; i < valueList.size(); i++) {
                ps.setObject(i+1,valueList.get(i));
            }
                rs = ps.executeQuery();
                while (rs.next()) {
                    list.add(cv());

                }
        }catch (Exception e){
        }
        return  list;
    }

    public UserBean cv() throws SQLException {
        UserBean user = new UserBean();
        user.setId(rs.getInt("pk_userId"));
        user.setBirthday(LocalDate.parse(rs.getString("u_birthday")));
        user.setName(rs.getString("u_name"));
        user.setLevel(rs.getString("u_level"));
        user.setPsw(rs.getString("u_psw"));
        return user;
    }


}
